const { Controller } = require('egg');
const jwt = require('jsonwebtoken');
class UserController extends Controller {
  async login(data) {
    const { ctx } = this;
    console.log(ctx, 'CTX');
    const { username, password } = data;

    const post = await this.app.mysql.get('user', { username }); // 在 posts 表中，插入 title 为 Hello World 的记录
    console.log(post, 'RESult');
    if (post) {
      const route = await this.app.mysql.select('router_copy', {
        where: {
          // eslint-disable-next-line no-use-before-define
          rlow: post.rlow,
        },
      });
      // eslint-disable-next-line no-undef, eqeqeq
      if (post.password == password) {
        const token = jwt.sign({
          data: 'foobar',
        }, 'secret', { expiresIn: 60 * 60 });

        return {
          code: 0,
          message: '登录成功————————',
          token,
          username,

          auth: route,
        };
      }
      return {

        code: 1,
        message: '登录失败————————',

      };


    }
    return {
      code: 1,
      message: '登录失败————————',
    };

  }


}

module.exports = UserController;
